package com.yzf.wisecoder.controller;

import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.ViewConfigReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.ViewConfigResp;
import com.yzf.wisecoder.wisecoderservice.service.ViewConfigService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewConfigEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-12 10:20:48
 */
@RestController
@RequestMapping("sys/viewconfig")
@AllArgsConstructor
@Api(value = "大屏展示配置控制", tags = "大屏展示配置控制")
public class ViewConfigController {

    private final ViewConfigService viewConfigService;

    /**
     * 大屏展示列表
     *
     * @param projectId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "大屏展示列表")
    public Response<List<ViewConfigResp>> list(@RequestParam Long projectId) {
        List<ViewConfigEntity> list = viewConfigService.queryList(projectId);
        List<ViewConfigResp> viewConfigResps = BeanCopyUtils.copyList(list, ViewConfigResp.class);
        return Response.buildSuccessResponseWithData(viewConfigResps);
    }


    /**
     * 查询大屏配置信息
     *
     * @param viewId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "查询大屏配置信息")
    public Response<ViewConfigResp> info(@RequestParam Long viewId) {
        ViewConfigEntity viewConfig = viewConfigService.getById(viewId);
        ViewConfigResp viewConfigResp = new ViewConfigResp();
        BeanUtils.copyProperties(viewConfig, viewConfigResp);
        return Response.buildSuccessResponseWithData(viewConfigResp);
    }

    /**
     * 查询大屏详细配置信息
     *
     * @param viewId
     * @return
     */
    @GetMapping("/info/config")
    @ApiOperation(value = "查询大屏详细配置信息")
    public Response<ViewConfigResp> infoConfig(@RequestParam Long viewId) {
        ViewConfigEntity viewConfig = viewConfigService.queryViewConfig(viewId);
        ViewConfigResp viewConfigResp = new ViewConfigResp();
        BeanUtils.copyProperties(viewConfig, viewConfigResp);
        return Response.buildSuccessResponseWithData(viewConfigResp);
    }

    /**
     * 保存或修改大屏配置
     *
     * @param viewConfig
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "保存或修改配置")
    public Response<Long> save(@RequestBody ViewConfigReq viewConfig) {
        ValidatorUtils.validateEntity(viewConfig);
        ViewConfigEntity viewConfigEntity = new ViewConfigEntity();
        BeanUtils.copyProperties(viewConfig, viewConfigEntity);
        viewConfigService.saveOrUpdateViewConfig(viewConfigEntity);

        return Response.buildSuccessResponseWithData(viewConfigEntity.getViewId());
    }

    /**
     * 删除大屏配置
     *
     * @param viewId
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除大屏配置")
    public Response delete(@RequestParam Long viewId) {
        viewConfigService.removeViewConfig(viewId);
        return Response.buildSuccessResponse();
    }

    /**
     * 模板发布
     *
     * @param viewId
     * @return
     */
    @GetMapping("/publish")
    @ApiOperation(value = "大屏发布")
    public Response publish(@RequestParam Long viewId) {
        viewConfigService.pushViewConfig(viewId);
        return Response.buildSuccessResponse();
    }

    /**
     * 取消大屏发布
     *
     * @param viewId
     * @return
     */
    @DeleteMapping("removePublish")
    @ApiOperation(value = "取消大屏发布")
    public Response removePublish(@RequestParam Long viewId) {
        viewConfigService.removePublish(viewId);
        return Response.buildSuccessResponse();
    }
}
